package com.aikon.wht.concurrent;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author haitao.wang
 */
public class Bank  {

    public static void main(String[] args) {
        Account account = new Account();
        account.setBalance(1000);
        CountDownLatch countDownLatch = new CountDownLatch(200);
        for (int i = 0 ;i<100;i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    account.addAmount(10);
                    System.out.println("add "+account.getBalance());
                    countDownLatch.countDown();
                }
            }).start();
        }
        for (int i = 0 ;i<100;i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    account.substractAmount(10);
                    System.out.println("sub "+account.getBalance());
                    countDownLatch.countDown();/**/
                }
            }).start();
        }
        try {
            countDownLatch.await(2,TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("fin "+account.getBalance());
        account.start();
    }
}
